//
//  HomeController.m
//  LeaveLight
//
//  Created by 张 on 2019/3/5.
//  Copyright © 2019年 xsl. All rights reserved.
//

#import "HomeController.h"


@interface HomeController () <iCardsDataSource, iCardsDelegate>

@property (nonatomic, nonatomic) iCards *cards;
@property (nonatomic, strong) NSMutableArray *cardsData;
@property (nonatomic, strong) TempView * tempView;
@end

@implementation HomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviViewHidden:YES];
    [self.view addSubview:self.cards];
    [self reloadCardsView];
}

- (TempView *)tempView
{
    if (!_tempView) {
        weakify(self);
        _tempView = [[TempView alloc]initWithFrame:CGRectMake(27, 50, SCREEN_WIDTH - 27*2, SCREEN_HEIGHT - 50 - 19 - TabBarHeight) reload:^{
            strongify(self);
            [self reloadCardsView];
        }];
    }
    return _tempView;
}

- (void)showTmpViewwWithStyle:(BOOL)hidden
{
    if (hidden) {
        [self.tempView removeFromSuperview];
    }else{
        [self.view addSubview:self.tempView];
    }
}

//请求数据
- (void)reloadCardsView
{
    self.cardsData = [NSMutableArray array];
    [QMUITips showLoadingInView:self.view];
    [self getLeavelightListWithcategoryid:@"" Success:^(NSMutableArray * _Nonnull listArr) {
        [QMUITips hideAllTips];
        [self.cardsData addObjectsFromArray:listArr];
        [self.cards reloadData];
        [self showTmpViewwWithStyle:YES];
    } failed:^(NSString * _Nonnull error) {
        [QMUITips hideAllTips];
        [QMUITips showWithText:error];
        if (CLIENT.isHaveNetwork == NO) {
            [self showTmpViewwWithStyle:NO];
        }
    }];
}

- (iCards *)cards
{
    if (!_cards) {
        _cards = [[iCards alloc]initWithFrame:CGRectMake(27, 50, SCREEN_WIDTH - 27*2, SCREEN_HEIGHT - 50 - 19 - TabBarHeight)];
        self.cards.dataSource = self;
        self.cards.delegate = self;
    }
    return _cards;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (NSMutableArray *)cardsData {
    if (_cardsData == nil) {
        _cardsData = [NSMutableArray array];
    }
    return _cardsData;
}

#pragma mark - iCardsDataSource methods

- (NSInteger)numberOfItemsInCards:(iCards *)cards {
    return self.cardsData.count;
}

- (UIView *)cards:(iCards *)cards viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
    TemplateModel *templateModel = [[TemplateModel alloc] init];
    templateModel.chicken_model = self.cardsData[index];
    NSInteger tag = templateModel.chicken_model.template_type;
    switch (tag) {
        case 1:
        case 2:
        case 3:
        case 6:{
            TempateCareOne * careView = [[TempateCareOne alloc]initWithFrame:CGRectMake(0, 0, cards.qmui_width, cards.qmui_height)];
            [careView setCardModel:templateModel];
            [self addTapWithView:careView withtag:index];
            return careView;
        }break;
        case 4:
        case 5:{
            TempateCareTwo * careView = [[TempateCareTwo alloc]initWithFrame:CGRectMake(0, 0, cards.qmui_width, cards.qmui_height)];
            [careView setCardModel:templateModel];
            [self addTapWithView:careView withtag:index];
            return careView;
        }break;
        case 7:
        case 8:{
            TempateCareThere * careView = [[TempateCareThere alloc]initWithFrame:CGRectMake(0, 0, cards.qmui_width, cards.qmui_height)];
            [careView setCardModel:templateModel];
            [self addTapWithView:careView withtag:index];
            return careView;
        }break;
        
        default:
            break;
    }
    return nil;
}

- (void)addTapWithView:(UIView *)view withtag:(NSInteger)tag
{
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchView:)];
    view.tag = tag;
    [view addGestureRecognizer:tap];
}

- (void)touchView:(UIGestureRecognizer *)gesView{
    TemplateModel *templateModel = [[TemplateModel alloc] init];
    templateModel.chicken_model = self.cardsData[gesView.view.tag];
    EditController * edit = [[EditController alloc]initWithNibName:@"EditController" bundle:nil];
    edit.model = templateModel;
    [self.navigationController pushViewController:edit animated:YES];
}

#pragma mark - iCardsDelegate methods

- (void)cards:(iCards *)cards beforeSwipingItemAtIndex:(NSInteger)index {
    NSLog(@"Begin swiping card %ld!", index);
}

- (void)cards:(iCards *)cards didLeftRemovedItemAtIndex:(NSInteger)index {
    NSLog(@"<--%ld", index);
}

- (void)cards:(iCards *)cards didRightRemovedItemAtIndex:(NSInteger)index {
    NSLog(@"%ld-->", index);
}

- (void)cards:(iCards *)cards didRemovedItemAtIndex:(NSInteger)index {
    NSLog(@"index of removed card: %ld", index);
    if (index == self.cardsData.count - 1) {
        [self reloadCardsView];
    }
}


@end
